// pages/activity/sixone/sixone.js
Page({
  data: {
    bottomHeight: getApp().globalData.bottomHeight,
    index: 1,
    list: [],
    joinNums: "",
    total: "",
    pageVisits: "",
    rank: [],
    page1: 1,
    size1: 10,
    isNoMore1: true,
    page2: 1,
    size2: 10,
    isNoMore2: true,
    isReport: false,
    my: {}
  },
  onLoad(options) {
    this.reset();
  },
  onShow() {},
  reset() {
    this.setData({
      list: [],
      rank: [],
      page1: 1,
      size1: 10,
      isNoMore1: true,
      page2: 1,
      size2: 10,
      isNoMore2: true
    });
    getApp().globalData.http.sixone_my().then(res => {
      if (!!res.data) {
        let arr = this.data.list;
        arr.unshift(res.data);
        this.setData({
          isReport: true,
          my: res.data,
          list: arr
        });
      }
    })
    this.getList();
    this.getRank();
  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
    this.reset();
  },
  onReachBottom() {
    if (this.data.index == 1) {
      if (this.data.isNoMore1) {
        this.setData({
          page1: this.data.page1 / 1 + 1
        }, () => {
          this.getList();
        });
      }
    } else if (this.data.index == 2) {
      if (this.data.isNoMore2) {
        this.setData({
          page2: this.data.page2 / 1 + 1
        }, () => {
          this.getRank();
        });
      }
    }
  },
  onShareAppMessage() {
    return {
      title: "晒萌娃，赢千元大奖，你也来看看~",
      imageUrl: "https://linxiaobang.oss-cn-beijing.aliyuncs.com/img/banner/WechatIMG5180.jpeg",
    }
  },
  navTab(e) {
    let val = e.currentTarget.dataset.index;
    this.setData({
      index: val
    });
    this.reset();
  },
  getList() {
    getApp().globalData.http.sixone_getballot({
      page: this.data.page1,
      size: this.data.size1,
    }).then(res => {
      let data_tmp = [...this.data.list, ...res.data];
      this.setData({
        joinNums: res.joinNums,
        total: res.total,
        pageVisits: res.pageVisits,
        list: data_tmp
      });
      if (res.data.length == 0) {
        this.setData({
          isNoMore1: false
        });
      }
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
    });
  },
  getRank() {
    getApp().globalData.http.sixone_rank({
      page: this.data.page2,
      size: this.data.size2,
    }).then(res => {
      let data_tmp = [...this.data.rank, ...res.data];
      this.setData({
        rank: data_tmp
      });
      if (res.data.length == 0) {
        this.setData({
          isNoMore2: false
        });
      }
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
    });
  },
  ballot(e) {
    getApp().globalData.vant.dialog.confirm({
        title: "确定投票?",
        zIndex: "500",
        message: "",
        confirmButtonText: "确定",
        confirmButtonColor: "#33B868"
      })
      .then(() => {
        getApp().globalData.http.sixone_ballot(e.currentTarget.dataset.id).then(res => {
          getApp().globalData.vant.toast.success("投票成功");
          let key1 = "list[" + e.currentTarget.dataset.index + "].ballotNums";
          let key2 = "list[" + e.currentTarget.dataset.index + "].ballotFlag";
          this.setData({
            [key1]: this.data.list[e.currentTarget.dataset.index].ballotNums / 1 + 1,
            [key2]: 1
          });
        }).catch(e => {
          getApp().globalData.vant.toast.fail(e.msg);
        });
      }).catch(() => {});
  },
  previewImg(e) {
    let img = e.currentTarget.dataset.img;
    let arr = [img];
    let current = img;
    getApp().globalData.util.previewImage(arr, current);
  }
})